<?php
namespace Shop\Api;
use Common\Api\Api;
use Shop\Api\OrderRefundApi;
use Shop\Model\OrderRefundMoneyModel;
use Shop\Model\OrdersModel;
use Shop\Model\OrdersRefundMoneyModel;

/**
 * Created by PhpStorm.
 * User: an
 * Date: 2015/8/20
 * Time: 17:38
 */
class OrderRefundMoneyApi extends  Api{
    const ADD="Shop/OrderRefundMoney/add";

    const QUERY="Shop/OrderRefundMoney/query";
	
	const QUERY_NO_PAGING="Shop/OrderRefundMoney/queryNoPaging";

    const SAVE_BY_ID="Shop/OrderRefundMoney/saveById";

    const SAVE="Shop/OrderRefundMoney/save";

    const DELETE="Shop/OrderRefundMoney/delete";

    const GET_INFO ="Shop/OrderRefundMoney/getInfo";

    const REFUND_SUCCESS ="Shop/OrderRefundMoney/refundSuccess";

    const WXREFUND_SUCCESS ="Shop/OrderRefundMoney/wxRefundSuccess";

    protected  function _init(){
        $this->model=new OrdersRefundMoneyModel();
    }

    public function refundSuccess($batch_no,$result_data){

        $map = array(
            'batch_no' => $batch_no,
        );

        $entity = array(
            'result_data' => $result_data,
            'result' => 1
        );
        $result = $this->model->where($map)->save($entity);

        if($result){
            return $this->apiReturnSuc('记录成功');
        }else{
            $error = $this -> model -> getDbError();
            return $this->apiReturnErr($error);
        }
    }
//add by zhouzhou
    //微信退款 支持分次
    public function wxRefundSuccess($batch_no,$result_data,$wxdata){

        $map = array('batch_no' => $batch_no);
        $r = apiCall(OrderRefundApi::GET_INFO,array($map));
        $data = $r['info']; 
//
        $entity = array(
            'order_code'     =>$data['order_code'],
            'reason'         =>$data['reason'],
            'result'         =>1,
            'create_time'    =>NOW_TIME,
            'result_data'    =>$result_data,
            'data'           =>'[微信退款单]'.$wxdata['refund_id'],
            'money'          =>number_format(intval($wxdata['refund_fee'])/100, 2, '.', ''),
            'currency'       =>($wxdata['fee_type']?$wxdata['fee_type']:'CNY'),
            'refund_channel' =>2,
            'batch_no'       =>$batch_no
        );
        // dump($entity);
        $result = $this-> model ->add($entity);

        if($result){
            return $this->apiReturnSuc('记录成功');
        }else{
            $error = $this -> model -> getDbError();
            return $this->apiReturnErr($error);
        }
    }
//end
}
